import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wu_you/data/cache/login_cache.dart';
import 'package:wu_you/model/home/mall_model.dart';
import 'package:wu_you/page/mall/page_product_exc_detail.dart';
import 'package:wu_you/page/splash/page_login.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/state/bloc/mall_bloc.dart';
import 'package:wu_you/utils/navigator_util.dart';
import 'package:wu_you/widgets/http_list_widget.dart';

///兑换
class ExchangePage extends StatefulWidget{
  @override
  ExchangePageState createState() => ExchangePageState();
}
class ExchangePageState extends State<ExchangePage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("兑换"),
      ),
      body: Padding(
        padding: EdgeInsets.all(10),
        child:HttpListWidget<MallProductModel, GroupProductBloc>(
          creator: (context) => GroupProductBloc(),
          onObtainData: (context, page) {
            BlocProvider.of<GroupProductBloc>(context).add(GroupProductEvent(page: page,goods_type: "2"));
          },
          successBuilder: (context, data) {
            return GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 4,
                mainAxisSpacing: 4,
                childAspectRatio: 0.73,
              ),
              itemCount: data.length,
              itemBuilder: (context, index) {
                return _buildProductItem(data[index]);
              },
            );
          },
        ),
      )
    );
  }

  Widget _buildProductItem(MallProductModel item) {
    return GestureDetector(
      onTap: () async {
        bool isLogin = await LoginCache().get();
        if(isLogin == null || !isLogin){
          NavigatorUtil.navigateToPage(context, LoginPage());
        }else{
          NavigatorUtil.navigateToPage(context, ExcProductDetailInfo(productId: item.goods_id,));
        }
      },
      child: Card(
        elevation: 4,
        child: PhysicalModel(
          color: Colors.transparent,
          clipBehavior: Clip.antiAlias,
          borderRadius: BorderRadius.circular(5),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              AspectRatio(
                aspectRatio: 1.1,
                child: Image.network(
                  "${item.img_url}",
                  fit: BoxFit.fill,
                ),
              ),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.only(left: 6,right:6),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Text(
                        "${item.name}",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(fontSize: 14, color: AppColors.textColor),
                      ),
                      Text(
                        "${item.title}",
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(fontSize: 12, color: AppColors.textColor),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            "无忧积分:${item.sell_price}",
                            style: TextStyle(fontSize: 12, color: AppColors.primaryColor),
                          ),
                          // Text(
                          //   "${item.sales_volume}${message['countBuy']}",
                          //   style: TextStyle(fontSize: 12, color: AppColors.textHintColor),
                          // ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}